import { OuterOreTask } from '../../game/global'
import { LogHelper } from "../../utils/logHelper";

export class OuterOreManagerFlagTask implements IFlagTask{
  run(flag: Flag): void {
    const room = Game.rooms[flag.memory.outerOre.room];
    const closestRoom = Game.rooms[flag.memory.outerOre.closestRoom];

    if(!room) {
      // 申请派出探路者
      if(!closestRoom.memory.spawnQueue){
        closestRoom.memory.spawnQueue = new Array<{ id:number,role:string,body:BodyPartConstant[],memory:CreepMemory }>();
      }
      // 查找当是否有探路者
      const queueCreeps = closestRoom.memory.spawnQueue.filter((creep)=>{
        return creep.memory.targetRoom === flag.memory.outerOre.room &&
          creep.memory.role === 'pathfinder';
      });
      const closestRoomCreeps = Object.values(Memory.creeps).filter((c)=> c.role === 'pathfinder' && c.targetRoom === flag.memory.outerOre.room);
      
      if(queueCreeps.length === 0 && closestRoomCreeps.length === 0 && !closestRoom.find(FIND_MY_SPAWNS)[0].spawning){
        closestRoom.spawnQueueAdd('pathfinder',[MOVE,MOVE,MOVE,MOVE,MOVE,MOVE],{role:'pathfinder',bornRoom:closestRoom.name,targetRoom:flag.pos.roomName});
        LogHelper.warn(`Flag[${flag.name}] 房间 ${flag.pos.roomName} 未有视野，申请派出探路者`);
        return;
      }
    }

    for(const task of OuterOreTask){
      task.run(room,closestRoom);
    }
  }
}